<template>
  <el-tabs type="border-card" class="content-tabs">
    <el-tab-pane label="会议室检查">
      <el-card class="content-card">
        <template #header>
          <div class="card-header">
            <span>会议室检查</span>
          </div>
        </template>

        <div class="search-form">
          <span>会议室状态：</span>
          <el-select v-model="selectValue" placeholder="全部(空)">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
          <el-button type="primary" @click="fetchRoomList()">搜索</el-button>
        </div>

        <div class="content-box">
          <el-scrollbar width="100%" max-height="450px"><!--这个不知道好不好-->
            <div class="room-content">
              <div class="room-box" 
                v-for="item in roomList" :key="item.roomId"
              >
                <span class="room-name">{{item.roomName}}</span>
                <span class="room-status">{{item.roomStatus}}</span>
                <span class="room-btn">              
                  <el-button type="primary" v-if="item.roomStatus=='使用'" @click="handleRoom(item,'维护')" round plain>在维护</el-button>
                  <el-button type="success" v-if="item.roomStatus=='使用'||item.roomStatus=='维护'" @click="handleRoom(item,'空闲')" round plain>已空闲</el-button>
                </span>
              </div>
            </div>
          </el-scrollbar>
        </div>
      </el-card>
    </el-tab-pane>
  </el-tabs>
</template>

<script setup>
  import { ref,reactive, onMounted } from 'vue'
  import { ElMessage } from 'element-plus';
  import { 
    GetRoomStatusServer,
    AudioRoomStatusServer
  } from '@/api/instance'

  onMounted(()=>{
    fetchRoomList();
  })

  const selectValue = ref('')
  const options=[
    { id:0 , value: '' ,label: '全部(空)' },
    { id:1 , value: '维护' , label: '维护' },
    { id:2 , value: '使用' , label: '使用' },
    { id:3 , value: '预定' , label: '预定' },
    { id:4 , value: '空闲' , label: '空闲' },
  ]
  const searchRoom=async () => {
    console.log(selectValue.value)
  }
  const roomList=reactive([
  ])
  const fetchRoomList=async () => {
    try{
      const res = await GetRoomStatusServer({roomStatus:selectValue.value});
      if(res.code===0){
        const allData=res.data||[];
        roomList.splice(0,roomList.length,...allData);
      }else{
        ElMessage.error(res.message || '获取房间列表失败')  
      }
    }catch(error){
      ElMessage.error(error.message || '网络请求失败1')
    }
  }
  const handleRoom =async (item,status) => {
    try{
      const res = await AudioRoomStatusServer({
        roomId:item.roomId,
        roomStatus:status
      })
      if(res.code===0){
        ElMessage.success('房间状态修改成功')
        fetchRoomList();
      }else{
        ElMessage.error(res.message || '房间状态修改失败')
      }
    }catch(error){
      ElMessage.error(error.message || '网络请求失败2')
    }
  }
</script>

<style lang="scss" scoped>
  .content-tabs{
    height: 95%;
    width: 95%;
    margin: 15px auto;//原来为20px，现在为15px
    .content-card{
      height: 95%;
      margin: 20px;
      box-sizing: border-box;
      .card-header{
        font-size: 18px;
        font-weight: bold;
      }
      .search-form{
        display: flex;
        justify-content: flex-start;
        align-items: center;
        margin-bottom: 15px;
        :deep(.el-select){
          width: 200px;
          margin-right: 20px;
        }
      }
      .content-box{
        display: flex;
        width: 100%;
        height: 100%;
        padding: 0px;
        overflow-y: hidden;
        padding-left: 15px;
        // flex:1;
        // min-height: 0;
        // :deep(.el-scrollbar__wrap){
        //   height: 100%;
        // }
        .room-content{
          display: flex;
          flex-direction: row;
          flex-wrap: wrap;
          box-sizing: border-box;
          justify-content: flex-start;
          width: 100%;
          height: 100%;
          gap: 30px;
          .room-box{
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            width: 200px;
            height: 100px;
            border: 1px solid rgb(161, 161, 161);
            padding:5px;
            border-radius: 20px;
            .room-name{
              font-size: 18px;
              font-weight: bold;
              line-height: 18px;
              margin-bottom: 10px;
            }
            .room-status{
              font-size:16px;
              line-height: 18px;
              margin-bottom: 10px;
            }
          }
          .room-box:hover{
            box-shadow: #ccc 2px 2px 5px;
          }
        }
      }
    }
  }
  .el-tab-pane {
    height:100%;
  }
  .el-form {
    padding: 20px;
  }
  .el-form-item {
    margin-bottom: 60px; /* 原默认值为 25px，可调大数值 */
    margin-right:45%;
    margin-left:5%;
  }
</style>